package net.liteheaven.xblib.common.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import net.liteheaven.xblib.common.rom.RomHelper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 顶部状态栏帮助类
 * Created by liangy on 2017/1/11.
 */
public class StatusBarHelper {

    // 背景色
    public static int COLOR_DARK = Color.parseColor("#009EE6");
    public static int COLOR_LIGHT = Color.parseColor("#FFFFFF");
    // 是否使能明暗模式改变文字和图标的颜色
    public static boolean ENABLE_CONTENT_MODE = false;
    public static boolean USE_M_BACKGROUND_METHOD = false;

    // 明暗模式：决定状态栏文字渲染的深浅
    public static final int LIGHT_MODE_NONE = 0;
    public static final int LIGHT_MODE_ANDROID_M = 1;
    public static final int LIGHT_MODE_MI = 2;
    public static final int LIGHT_MODE_FLYME = 3;

    private static int getContentMode(){
        int result = LIGHT_MODE_NONE;
        if (RomHelper.isMiui()){
            result = LIGHT_MODE_MI;
        }else if (RomHelper.isFlyme()){
            result = LIGHT_MODE_FLYME;
        }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            result = LIGHT_MODE_ANDROID_M;
        }
        return result;
    }

    /**
     *
     * @param activity
     * @param dark 把文字设置成深色
     * @return
     */
    public static boolean setStatusBarMode(Activity activity, boolean dark){

        // step 1. 根据模式调整状态栏状态
        boolean result = false;
        if (ENABLE_CONTENT_MODE){
            int mode = getContentMode();
            switch (mode){
                case LIGHT_MODE_NONE:
                    break;
                case LIGHT_MODE_ANDROID_M:
                    result = setStatusBarContentModeAndroidM(activity.getWindow(), dark);
                    break;
                case LIGHT_MODE_MI:
                    result = setStatusBarContentModeMiui(activity.getWindow(), dark);
                    break;
                case LIGHT_MODE_FLYME:
                    result = setStatusBarContentModeFlyme(activity.getWindow(), dark);
                    break;
            }
        }else{
            result = true;
        }

        // step 2. 如果能够成功修改图标和文字的模式，则继续修改背景色与之匹配
        if (result){
            if (dark){
                StatusBarHelper.setStatusBarBackgroundColor(activity, COLOR_LIGHT);
            }else{
                StatusBarHelper.setStatusBarBackgroundColor(activity, COLOR_DARK);
            }
        }
        return result;
    }

    /**
     * 修改状态栏颜色，支持4.4以上版本
     * @param activity
     * @param color
     */
    private static boolean setStatusBarBackgroundColor(Activity activity,int color) {
        boolean result = false;
        if (USE_M_BACKGROUND_METHOD){
            /**
             * 使用这段会带上安卓默认的蒙层，特别在MIUI上面丑的一比
             */
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                setStatusBarColorM(activity, color);
                result = true;
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                setStatusBarColorK(activity, color);
                result = true;
            }
        }else{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                setStatusBarColorK(activity, color);
                result = true;
            }
        }
        return result;
    }

    private static void setStatusBarColorM(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
        }
    }

    private static void setStatusBarColorK(Activity activity,int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //使用SystemBarTint库使4.4版本状态栏变色，需要先将状态栏设置为透明
            SystemBarTintManager tintManager = new SystemBarTintManager(activity);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintColor(color);
        }
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为Flyme用户
     * @param window 需要设置的窗口
     * @param dark 是否把状态栏字体及图标颜色设置为深色
     * @return  boolean 成功执行返回true
     *
     */
    private static boolean setStatusBarContentModeFlyme(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception e) {

            }
        }
        return result;
    }

    /**
     * 设置状态栏字体图标为深色，需要MIUIV6以上
     * @param window 需要设置的窗口
     * @param dark 是否把状态栏字体及图标颜色设置为深色
     * @return  boolean 成功执行返回true
     *
     */
    private static boolean setStatusBarContentModeMiui(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field  field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if(dark){
                    extraFlagField.invoke(window,darkModeFlag,darkModeFlag);//状态栏透明且黑色字体
                }else{
                    extraFlagField.invoke(window, 0, darkModeFlag);//清除黑色字体
                }
                result=true;
            }catch (Exception e){

            }
        }
        return result;
    }

    private static boolean setStatusBarContentModeAndroidM(Window window, boolean dark){
        if (!dark){
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }else{
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        return true;
    }
}
